//
//  Tree.cpp
//  Sinnca Engine v2
//
//  Created by Ryan Oldis on 7/28/11.
//  Copyright 2011 Sinnca Studios. All rights reserved.
//

#include "Node.h"


Tree* Tree::_instance = NULL;

Tree* Tree::Instance()
{
	if (_instance == NULL)
	{
		_instance = new Tree();
	}
	
	return _instance;
}


Tree::Tree()
{
	root = NULL;
	currentScene = NULL;
}

void Tree::setup(lua_State *L)
{
	//lua_getlocal(L, "scene");
	
	
	lua_newtable(L);
	
	lua_pushvalue(L, 1);
	lua_setmetatable(L, -2);
	
	lua_pushvalue(L, 1);
	lua_setfield(L, 1, "__index");
	 
	
	root = (scene**)lua_newuserdata(L, sizeof(scene*));
	*root = new scene("root");
	
	
	lua_getglobal(L, "scene");
	lua_setmetatable(L, -2);
	
	
	
	luaL_getmetatable(L, "scene");
	lua_setmetatable(L, -2);
	
	lua_setfield(L, -2, "__self");
	
	
	lua_setglobal(L, "root");
	
	
	currentScene = *((scene**)root);
}


scene** Tree::getRoot()
{
	//scene** r;
	return root;
}